import 'package:flutter/material.dart';

class LoadingButton extends StatefulWidget {
  final String buttonText;
  final VoidCallback? onPressed;
  final double fWidth;
  final double fHeight;
  final Color bgColor;
  final bool bIsRuning;

  const LoadingButton({
    super.key,
    this.buttonText = 'Text',
    this.onPressed,
    this.fWidth = 140,
    this.fHeight = 46,
    this.bgColor = Colors.blue,
    this.bIsRuning = false,
  });

  @override
  LoadingButtonState createState() => LoadingButtonState();
}

class LoadingButtonState extends State<LoadingButton>
    with SingleTickerProviderStateMixin {
  bool isLoading = false;
  late AnimationController _controller;
  bool isAnimating = false;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );

    if (widget.bIsRuning) {
      startLoading();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  //停止loading
  void stopLoading() {
    setState(() {
      isLoading = false;
    });
    _controller.reset();
    isAnimating = false;
  }

  //开始loading
  void startLoading() {
    if (!isLoading && !isAnimating) {
      setState(() {
        isLoading = true;
      });
      _controller.repeat();
      isAnimating = true;
    }

    // Future.delayed(const Duration(seconds: 12), () {
    //   stopLoading();
    // });
  }

  void _onPressed() {
    if (widget.onPressed != null) {
      widget.onPressed!();
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: isLoading ? null : () => _onPressed(),
      child: Container(
        width: widget.fWidth,
        height: widget.fHeight,
        padding: const EdgeInsets.only(left: 5),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6),
          color: widget.bgColor,
          // boxShadow: [
          //   BoxShadow(
          //     color: widget.bgColor.withOpacity(0.5),
          //     blurRadius: 5,
          //     spreadRadius: 2,
          //     offset: const Offset(0, 3),
          //   ),
          // ],
        ),
        child: isLoading
            ? Row(
                children: [
                  const SizedBox(width: 2),
                  RotationTransition(
                    turns: Tween(begin: 0.0, end: 1.0).animate(_controller),
                    child: const Icon(Icons.refresh, color: Colors.white),
                  ),
                  const SizedBox(width: 4),
                  Expanded(
                      child: Text(
                    widget.buttonText,
                    style: const TextStyle(color: Colors.white, fontSize: 16),
                  )),
                  const SizedBox(width: 2),
                ],
              )
            : Center(
                child: Text(
                  widget.buttonText,
                  style: const TextStyle(color: Colors.white, fontSize: 16),
                ),
              ),
      ),
    );
  }
}
